package cn.itcast.health.provider.service.impl;

import cn.itcast.health.commons.entity.Result;
import cn.itcast.health.commons.resources.MessageConstant;
import cn.itcast.health.interfaces.service.UserMobileService;
import cn.itcast.health.mapper.UserMobileMapper;
import cn.itcast.health.pojo.Member;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@Service
public class UserMobileServiceImpl implements UserMobileService {

    @Autowired
    private UserMobileMapper userMobileMapper;

    @Override
    public Result login(String telephone) {
        Result result = null;
        Member member = new Member();
        member.setPhoneNumber(telephone);
        member.setRegTime(new Date());
        try {
            Member member1  =  userMobileMapper.findByPhoneMember(member.getPhoneNumber());
            if(member1 == null){
                userMobileMapper.addMember(member);
                member1 = userMobileMapper.findByIdMember(member.getId());
            }
            result = new Result(true, MessageConstant.LOGIN_SUCCESS, JSON.toJSONString(member1));
        }catch (Exception e){
            e.printStackTrace();
            result = new Result(false,MessageConstant.LOGIN_ERROR);
        }
        return result;
    }
}
